extends CharacterBody2D
class_name Character

signal energy_change(cur_energy:int,max_energy:int)

@export_group("Move")
@export var jump_speed: float
@export var gravity: float
@export var speed: float
@export var acceleation: float
@export var dash_speed: float
@export var no_gravity: bool
@export var low_gravity: bool

@export_group("Energy")
@export var max_energy: int

@export_category("Prelode")
@export var Canying: PackedScene
#@onready var health_component: HealthComponent = %HealthComponent
@onready var body: Node2D = %Body
@onready var health_component: HealthComponent = %HealthComponent

var is_dash: bool = false
var is_pause: bool = false
var heading: Vector2 = Vector2.RIGHT

var can_dash: bool = true
var can_move: bool = true
var can_jump: bool = true
## Energy
var current_energy := 0:
	set(v):
		current_energy = clampi(v,0,max_energy)
		energy_change.emit(current_energy, max_energy)




func change_heading() -> void:
	if velocity.x > 0:
		body.scale.x = abs(body.scale.x)
		heading = Vector2.RIGHT
	elif velocity.x < 0:
		body.scale.x = -abs(body.scale.x)
		heading = Vector2.LEFT



func _process(delta: float) -> void:
	change_heading()

func _physics_process(delta: float) -> void:
	apply_gravity(delta)
	move_and_slide()






func apply_gravity(delta: float) -> void:
	if no_gravity: return
	if low_gravity:
		velocity.y += 0.1 * gravity * delta
	else:
		velocity.y += gravity * delta



func set_dash(bo: bool):
	can_dash = bo


func set_move(bo: bool):
	can_move = bo
	





func set_jump(bo: bool):
	can_jump = bo


func set_paused(bo: bool):
	is_pause = bo
	set_dash(!bo)
	set_jump(!bo)
	set_move(!bo)


func gain_energy(amount: float):
	current_energy = min(current_energy + amount, max_energy)

func lose_energy(amount: float):
	current_energy = max(current_energy - amount, 0)



	
